package gxwl.busins.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import gxwl.busins.entity.UploadEntity;
import gxwl.busins.mapper.UploadMapper;
import gxwl.busins.vo.DataListCondition;
import gxwl.busins.vo.ProcessVO;
import gxwl.busins.service.UploadService;
import gxwl.common.page.PageCondition;
import gxwl.common.page.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UploadServiceImpl extends ServiceImpl<UploadMapper, UploadEntity> implements UploadService {

    private final UploadMapper uploadMapper;

    @Autowired
    public UploadServiceImpl(UploadMapper uploadMapper) {
        this.uploadMapper = uploadMapper;
    }

    @Override
    public boolean process(ProcessVO processVO) {
        UploadEntity build = new UploadEntity();
        BeanUtil.copyProperties(processVO, build);
        build.setCreationTime(LocalDateTime.now());
        return this.save(build);
    }

    @Override
    public PageList<ProcessVO> list(PageCondition pageCondition,DataListCondition searchCondition) {
        Page<UploadEntity> page = ChainWrappers.lambdaQueryChain(uploadMapper)
                .eq(ObjectUtil.isNotNull(searchCondition.getExamDate()), UploadEntity::getExamDate, searchCondition.getExamDate())
                .eq(ObjectUtil.isNotNull(searchCondition.getRouteNo()), UploadEntity::getRouteNo, searchCondition.getRouteNo())
                .page(pageCondition.toPage(UploadEntity.class));
        return PageList.of(page,ProcessVO.class);
    }
}
